//
//  HomeSlideHeader.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/10.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "HomeSlideHeader.h"

#import "LXLoginViewController.h"

#import "LXMallShareController.h"

#import "LXMineViewController.h"
@interface HomeSlideHeader()
@property(nonatomic,strong)LXUser * user;

@end
@implementation HomeSlideHeader
-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.user = [LXUser shareUser];
        UIImageView * user_icon = [[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:user_icon];
        self.user_icon = user_icon;
        user_icon.width = Ratio_home_slide_header_user_icon_width * self.width;
        user_icon.height = user_icon.width;
        user_icon.x = Ratio_home_slide_header_user_icon_marginLeft * self.width;
        user_icon.y = Ratio_home_slide_header_user_icon_marginTop * self.height;
        [user_icon addRadius:user_icon.width*0.5];
        user_icon.image = [UIImage imageNamed:@"user_icon_default"];

        user_icon.userInteractionEnabled = YES;
        [user_icon addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(user_iconClick:)]];
        
        UIButton * share = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:share];
        self.share = share;
        share.width = Ratio_home_slide_header_user_share_width * self.width;
        share.height = share.width;
        share.centerY = user_icon.centerY;
        share.right = Ratio_home_slide_header_user_icon_marginLeft * self.width;
        [share setImage:[UIImage imageNamed:@"share_default"] forState:UIControlStateNormal];
        [share addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UILabel * user_name = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:user_name];
        self.user_name = user_name;
        user_name.backgroundColor = [UIColor clearColor];
        user_name.textColor = [UIColor orangeColor];
        user_name.font = [UIFont  systemFontOfSize:ISBigScreen?15:13];
        user_name.textAlignment = NSTextAlignmentCenter;
        user_name.width = share.left - user_icon.right;
        user_name.height = Ratio_home_slide_header_user_name_height * self.height;
        user_name.x = user_icon.right;
        user_name.centerY = user_icon.centerY;
        user_name.text = @"您还没有登录";
        user_name.userInteractionEnabled = YES;
        [user_name addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(user_nameClick:)]];
        UIView * underLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-0.5, self.width, 0.5)];
        underLine.backgroundColor = HexColor(@"#000000");
        [self addSubview:underLine];
    }
    return self;
}

-(void)user_iconClick:(UITapGestureRecognizer*)gesture{
    LXLoginViewController  * longVC = [[LXLoginViewController alloc] init];
    [[self getCurrentVC]presentViewController:longVC animated:YES completion:nil];

    if([self.delegate respondsToSelector:@selector(HomeSlideHeader:DidClickUserIcon:)]){
        [self.delegate HomeSlideHeader:self DidClickUserIcon:self.user_icon];
    }
}

-(void)user_nameClick:(UITapGestureRecognizer*)gesture{
    LXLoginViewController * logiv = [[LXLoginViewController alloc] initWithNibName:@"LXLoginViewController" bundle:nil];
    [[self getCurrentVC] presentViewController:logiv animated:YES completion:nil];
    if([self.delegate respondsToSelector:@selector(HomeSlideHeader:DidClickUserName:)]){
        [self.delegate HomeSlideHeader:self DidClickUserName:self.user_name];
    }
}

-(void)shareClick:(UIButton*)gesture{
     NSLog(@"分享点击事件");
    LXMallShareController * shareVC = [[LXMallShareController alloc] init];
    [[self getCurrentVC].navigationController pushViewController:shareVC animated:YES];
    if([self.delegate respondsToSelector:@selector(HomeSlideHeader:DidClickShare:)]){
        [self.delegate HomeSlideHeader:self DidClickShare:self.share];
    }
}
-(void)upDate{
    if(LoginState){
        self.user_name.text = [NSUserDefaults stringValueForKey:KeyOfUserNickname].length==0?[NSUserDefaults stringValueForKey:KeyOfUserMobile]:[NSUserDefaults stringValueForKey:KeyOfUserNickname];
        [self.user_icon sd_setImageWithURL:[NSURL URLWithString:[NSUserDefaults stringValueForKey:KeyOfUserHeaderImage]] placeholderImage:[UIImage imageNamed:@"user_icon_default"]];
    }else{
        self.user_name.text = @"您还没有登录";
        self.user_icon.image = [UIImage imageNamed:@"user_icon_default"];
    }
}
@end
